﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class pim_EmergencyContact : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int per_id = 0;
        int emp_id = 0;
        int id = 0;
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["id"] != null) id = int.Parse(Request.QueryString["id"].ToString());
            if (Request.QueryString["per_id"] != null) per_id = int.Parse(Request.QueryString["per_id"].ToString());
            if (Request.QueryString["emp_id"] != null) emp_id = int.Parse(Request.QueryString["emp_id"].ToString());

            lb_per_id.Text = per_id.ToString();
            lb_emp_id.Text = emp_id.ToString();

            if (id != 0) {
                RE_GenericBean emergency = DB.getEmergency(id);
                lb_id.Text=emergency.intC1.ToString();
                lb_per_id.Text=emergency.intC2.ToString();
                tb_name.Text=emergency.strC1;
                tb_relacion.Text=emergency.strC2;
                tb_addr1.Text=emergency.strC3;
                tb_addr2.Text=emergency.strC12;
                tb_city_em.Text=emergency.strC4;
                tb_state_em.Text=emergency.strC5;
                tb_zip_em.Text=emergency.strC6;
                tb_country_em.Text=emergency.strC7;
                tb_telefono_em.Text=emergency.strC8;
                tb_email_em.Text=emergency.strC11;
                habilitocampos(false);
            }
        }
    }
    protected void Button7_Click(object sender, EventArgs e)
    {
        RE_GenericBean emergencyinf = new RE_GenericBean();
        bool actualiza = false;

        emergencyinf.intC1 = int.Parse(lb_id.Text);//id
        emergencyinf.intC2 = int.Parse(lb_per_id.Text);//codigo persona
        emergencyinf.strC1 = tb_name.Text.Trim();
        emergencyinf.strC2 = tb_relacion.Text.Trim();
        emergencyinf.strC3 = tb_addr1.Text.Trim();
        emergencyinf.strC4 = tb_addr2.Text.Trim();
        emergencyinf.strC5 = tb_city_em.Text.Trim();
        emergencyinf.strC6 = tb_state_em.Text.Trim();
        emergencyinf.strC7 = tb_zip_em.Text.Trim();
        emergencyinf.strC8 = tb_country_em.Text.Trim();
        emergencyinf.strC9 = tb_telefono_em.Text.Trim();
        emergencyinf.strC10 = tb_email_em.Text.Trim();

        if (emergencyinf.intC1 != 0) actualiza = true;
        RE_GenericBean res = new RE_GenericBean();
        res= DB.InsertaEmergencyInfo(emergencyinf, actualiza);
        if (res.boolC1) {
            Response.Redirect("PersonalInfo.aspx?emp_id=" + lb_emp_id.Text + "&tabindex=2");
        }
    }
    protected void Button8_Click(object sender, EventArgs e)
    {
        habilitocampos(true);
    }
    protected void Button9_Click(object sender, EventArgs e)
    {
        int id = 0;
        id = int.Parse(lb_id.Text);
        if (id != 0)
        {
            int result = DB.DeleteEmergencyContact(id);
            Response.Redirect("PersonalInfo.aspx?emp_id=" + lb_emp_id.Text+"&tabindex=2");
        }
    }

    private void habilitocampos(bool estado)
    {
        tb_name.Enabled = estado;
        tb_relacion.Enabled = estado;
        tb_addr1.Enabled = estado;
        tb_addr2.Enabled = estado;
        tb_city_em.Enabled = estado;
        tb_state_em.Enabled = estado;
        tb_zip_em.Enabled = estado;
        tb_country_em.Enabled = estado;
        tb_telefono_em.Enabled = estado;
        tb_email_em.Enabled = estado;
    }
}